// pages/index/index.js
import request from '../../utils/request'
Page({
  data: {
    bannerList:[],
    recommendList:[],
    topList:[],
  },
  onLoad(options) {
    this.getBannerList()
    this.getRecommendList()
    this.getTopList()
  },
  goRecommend() {
    wx.navigateTo({
      url: '/songPackage/pages/recommendSong/recommendSong',
    })
  },
  goSong() {
    wx.showToast({
      title: '功能待完善，敬请期待',
      icon:'none'
    })
  },
  goTv() {
    wx.showToast({
      title: '功能待完善，敬请期待',
      icon:'none'
    })
  },
  goNow() {
    wx.showToast({
      title: '功能待完善，敬请期待',
      icon:'none'
    })
  },
  goOther() {
    wx.showToast({
      title: '功能待完善，敬请期待',
      icon:'none'
    })
  },
  goMusic(e) {
    let songId = e.currentTarget.dataset.music.id
    wx.navigateTo({
      url: '/songPackage/pages/songDetail/songDetail?songId='+songId,
    })
  },
  // 轮播图列表
  async getBannerList() {
    try {
      let res = await request("banner", {
        type: 2
      })
      this.setData({
        bannerList: res.banners
      })
    } catch (error) {
      console.log(error);
    }
  },
  async getRecommendList() {
    try {
      let res = await request("personalized", {
        limit: 10
      })
      this.setData({
        recommendList: res.result
      })
    } catch (error) {
      console.log(error);
    }
  },
  async getTopList() {
    let index = 0
    let filalList = []
    while(index < 5){
      try {
        let res = await request("top/list", {
          idx: index ++
        })
        let topListItem = {
          name:res.playlist.name,
          tracks:res.playlist.tracks.slice(0, 3)
        }
        filalList.push(topListItem)
        this.setData({
          topList:filalList
        })
      } catch (error) {
        console.log(error);
      }
    }
  },
  onShareAppMessage() {},
  onShareTimeline() {}
})